package org.example.MVC;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class DataController {
    @RequestMapping("/showDataByResponse")
    public void showDataByResponse(HttpServletResponse response){
        try {
            response.setCharacterEncoding("gbk");    //防止乱码
            response.getWriter().print("response");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/showDataByJSON")
    public void showDataByJSON(HttpServletResponse response){
        try {
            ObjectMapper om = new ObjectMapper();
            User user = new User();
            user.setUsername("heima");
            user.setPassword("666");
            String ujson = om.writeValueAsString(user);
            response.getWriter().print(ujson);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
